import wepy from 'wepy'
export default class extends wepy.mixin {
	data = {
		// checked: true,
		isShow: true,
		userId: '',
		// 购物车修改数据
		cartList: [],
		//购物车原有数据
		urls: wepy.$instance.globalData.baseURl,
		zhanweit: '2020-05-07/202005071101371588820497192_n7j6r.png',
		iscount: 0,
		youhuiprice: 0,
		wuyefei: 0,
		manzengGoods: '',
		wenzi: '管理',
		show: false,
		yhshow: false,
		gglist: '',
		disabled: false,
	}
	onLoad() {

	}
	onShow() {
		if (wepy.getStorageSync('userId')) {
			let userid = wepy.getStorageSync('userId')
			this.userId = userid
			this.getCartList()
			this.youhuiprice = 0
			this.iscount = 0
			this.wuyefei = 0
			this.manzengGoods = ''
			this.isShow = true;
			this.wenzi = "管理";
		} else {
			wx.navigateTo({
				url: '../../pages/Authorization/authorization',
			})
		}
	}
	onHide() {
		this.show = false
		this.yhshow = false
	}
	methods = {
		// 点击管理
		administer() {
			if (this.isShow) {
				this.wenzi = "完成"
			} else {
				this.wenzi = "管理"
			}
			this.isShow = !this.isShow
		},
		async handle1(id, event) {
			console.log(id, "00000000000")
			console.log(event, "11111111")
			const {
				position,
				instance
			} = event.detail;
			switch (position) {
				case 'left':
				case 'cell':
					instance.close();
					break;
				case 'right':
					const res = await wepy.showModal({
						title: '提示',
						content: '确定要删除吗?',
					})
					if (res.confirm) {
						const res = await wepy.post('baseRest/deleteShoppingCart', {
							ids: id,
						})
						console.log(res)
						if (res.data.state == 1) {
							wepy.noToast('删除成功')
							instance.close();
							this.getCartList()
						} else if (res.cancel) {
							console.log('用户点击取消')
						}
					}
					break;
			}
		},
		// 去逛逛
		guangGuang() {
			console.log('去逛逛')
			wepy.switchTab({
				url: '../../pages/tabs/home',
			})
		},
		async onSubmit() {
			var cartList = this.cartList
			console.log(cartList, "============")
			var id = ''
			var num = ''
			for (var i = 0; i < cartList.length; i++) {
				if (cartList[i].isCheck) {
					id += cartList[i].id + ','
					num += cartList[i].num + ','
				}
			}
			id = id.substr(0, id.length - 1)
			num = num.substr(0, num.length - 1)
			if (id == '') {
				wepy.noToast('请选择要结算的商品')
			} else {
				wepy.navigateTo({
					url: '../../pages/confirmOrder/confirmorder?cart_id_list=' +
						id +
						'&cart_num_list=' +
						num +
						'&state=' +
						0 + '&numory=' + this.youhuiprice + '&isPoint=' + '0',
				})
			}
		},
		//监听数量加减
		countChange(e) {
			console.log("jiajian")
			console.log(e, '===============pppppppppppppp')
			let count = e.detail
			count = Number(count)
			let gid = e.target.dataset.id
			var maxnum = e.target.dataset.maxnum
			var cartList = this.cartList;
			if (count > maxnum) {
				return wepy.noToast('此规格商品库存不足')
				this.numAvtive(maxnum, gid, isCheck, e.target.dataset.skuid)
			} else {
				var cartList = this.cartList
				var isCheck = '';
				for (var i = 0; i < cartList.length; i++) {
					if (cartList[i].id == gid) {
						cartList[i].num = count
						isCheck = cartList[i].isCheck;
					}
				}
				this.numAvtive(count, gid, isCheck, e.target.dataset.skuid)
			}
		},
		goDatil(spuid, skuAttrValueIds, num, shoppingCartId) {
			console.log("---4556565",spuid, skuAttrValueIds, num,shoppingCartId)
			wepy.navigateTo({
				url: '../../pages/goodsDatils/gooddatils?id=' + spuid + '&skuAttrValueIds=' + skuAttrValueIds +
					'&shoppingCartId=' + shoppingCartId,
			})
		},
		// 商品前面的复选框发生改变
		statuChange(e) {
			var cartArr = this.cartList
			//商品id
			let gid = e.target.dataset.id
			const indexs = this.cartList.findIndex((x) => x.id === gid)
			if (indexs != -1) {
				this.cartList[indexs].isCheck = e.detail
			}
			//是否选中
			if (e.detail) {
				this.jisuanManjian()
			} else {
				this.jisuanManjian()
			}
		},
		// 全选状态
		statuAllChange(e) {
			this.cartList.forEach((x) => {
				//判断上下架
				if (x.isPub == 1) {
					x.isCheck = false;
				} else {
					x.isCheck = e.detail
				}
			})
			//是否全选
			if (e.detail) {
				this.jisuanManjian()
			} else {
				this.youhuiprice = 0
				this.iscount = 0
				this.wuyefei = 0
				this.manzengGoods = ''
			}
		},
		// 删除商品
		async onClickDelete() {
			var that = this
			var cartList = that.cartList
			console.log("删除商品", cartList)
			var id = ''
			for (var i = 0; i < cartList.length; i++) {
				if (cartList[i].isCheck) {
					id += cartList[i].id + ','
				}
			}
			id = id.substr(0, id.length - 1)
			console.log(id)
			if (id == '') {
				wepy.noToast('请选择要删除的商品')
			} else {
				const res = await wepy.showModal({
					title: '删除',
					content: '确定删除吗？',
				})
				if (res.confirm) {
					const res = await wepy.post('baseRest/deleteShoppingCart', {
						ids: id,
					})
					console.log(res)
					if (res.data.state == 1) {
						wepy.noToast('删除成功')
						that.isShow = true;
						that.wenzi = "管理";
						this.youhuiprice = 0
						this.iscount = 0
						this.wuyefei = 0
						this.manzengGoods = ''
						setTimeout(function() {
							that.getCartList()
						}, 2000)
					}
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		},
		showPopup(item) {
			if (item.isCheck == false) {
				this.show = true
				this.gglist = item
			}
			console.log("wwwwwwww", item)
		},

		onClose() {
			this.show = false
		},
		//点击已优惠
		youhui() {
			this.yhshow = true
		},
		yhclose() {
			this.yhshow = false
		},
		// 确认规格
		qrgg(item, spuId, id) {
			var ppmm = this.gglist;
			console.log(ppmm, "ttttt")
			if (ppmm.max_num == 0) {
				this.show = true;
				return wepy.noToast('此规格商品库存不足,请重新选择')
			} else {
				this.show = false;
				var goodsTitle = '';
				var skuAttrValueIds = '';
				var list = ppmm.attr_group_list
				var cartList = this.cartList;
				for (var i = 0; i < list.length; i++) {
					for (var j = 0; j < list[i].attrValues.length; j++) {
						if (list[i].attrValues[j].checked == true) {
							goodsTitle += list[i].attrValues[j].attrValueName + ''
							skuAttrValueIds += list[i].attrValues[j].attrValueId + ','
						}
					}
				}
				ppmm.skuAttrCodeNames = goodsTitle;
				ppmm.skuAttrValueIds = skuAttrValueIds.substr(0, skuAttrValueIds.length - 1);
				for (var k = 0; k < cartList.length; k++) {
					if (cartList[k].id == id) {
						cartList[k] = ppmm;
					}
				}
				this.numAvtive(ppmm.num, ppmm.id, ppmm.isCheck, ppmm.skuAttrValueIds)
				this.gglist = ppmm
				this.cartList = cartList;
				this.$apply()
			}
		},
		titleActive(item, index1, itemp, gglist, index2) {
			console.log(item, index1, itemp)
			for (var i = 0; i < itemp.attrValues.length; i++) {
				if (i == index2) {
					itemp.attrValues[i].checked = true
				} else {
					itemp.attrValues[i].checked = false
				}
			}
			gglist.attr_group_list[index1] = itemp
			console.log(gglist)
			//sku组合
			var idslist = '';
			for (var i = 0; i < gglist.attr_group_list.length; i++) {
				for (var j = 0; j < gglist.attr_group_list[i].attrValues.length; j++) {
					if (gglist.attr_group_list[i].attrValues[j].checked) {
						idslist += gglist.attr_group_list[i].attrValues[j].attrValueId + ','
					}
				}
			}
			//选中的sku组合
			idslist = idslist.substr(0, idslist.length - 1)
			console.log(idslist)
			this.gglist = gglist;
			this.editguige(gglist, gglist.spuId, 1, idslist, gglist.id)
		},
		fangBig(current) {
			console.log('current', current)
			var lists = []
			lists.push(current)
			wepy.previewImage({
				// 所有图片的路径
				urls: lists.map((x) => x),
				// 当前默认看到的图片
				current: current,
			})
		},

	}
	computed = {
		// 判断页面是否有商品加入
		isEmpty() {
			if (this.cartList.length <= 0) {
				return true
			}
			return false
		},
		// 判断是否全选
		isFullCheck() {
			const allCount = this.cartList.length
			let c = 0
			this.cartList.forEach((x) => {
				if (x.isPub == 1) {
					if (x.isCheck) {
						c++
					} else {
						c++
					}
				} else {
					if (x.isCheck) {
						c++
					}
				}
			})
			return allCount === c
		},
		amout() {
			return this.cartList.length
		},
	}
	async gengxin(num, id, skuAttrValueIds) {
		const {
			data: res
		} = await wepy.post('baseRest/updateShoppingCart', {
			num: JSON.stringify(num),
			id: id,
			skuAttrValueIds: skuAttrValueIds
		})
		var that = this;
		if (res.state == 0) {
			wepy.noToast(res.message)
		} else {
			that.getCartList()
		}
	}
	// 选择规格
	async editguige(gglist, spuId, goodsNum, skuAttrValueIds, id) {
		const res = await wepy.post('baseRest/getChooseGoodsSku', {
			id: spuId,
			num: JSON.stringify(goodsNum),
			skuAttrValueIds: skuAttrValueIds,
		})
		if (res.data.state == 1) {
			var that = this;
			var data = res.data.data
			if (data.skuNum == 0) {
				that.disabled = true
				gglist.max_num = data.skuNum
				gglist.imagePath = data.skuImg
				gglist.price = data.skuPrice
				gglist.skuAttrCodeNames = data.skuAttrCodeNames
				gglist.skuAttrValueIds = data.skuAttrValueIds
				if (data.num > data.skuNum) {
					gglist.num = 1
				} else {
					gglist.num = data.num
				}
				console.log(gglist, "22222222")
				that.gglist = gglist
				that.$apply()
				return wepy.noToast('此规格商品库存不足,请重新选择')
			} else {
				that.disabled = false
				gglist.max_num = data.skuNum
				gglist.imagePath = data.skuImg
				gglist.price = data.skuPrice
				gglist.skuAttrCodeNames = data.skuAttrCodeNames
				gglist.skuAttrValueIds = data.skuAttrValueIds
				if (data.num > data.skuNum) {
					gglist.num = 1
				} else {
					gglist.num = data.num
				}
				console.log(gglist, "22222222")
				that.gglist = gglist
				that.$apply()
			}
		} else {
			return wepy.noToast(res.data.message)
		}
	}
	// 获取购物车数据
	async getCartList() {
		const res = await wepy.post('baseRest/getShoppingCartList', {
			memberId: this.userId,
		})
		if (res.statusCode != 200) {
			return wepy.noToast('获取数据失败，请检查网络')
		}
		console.log(res, "============")
		let cartArr = res.data.data
		let NewArr = []
		if (cartArr != undefined) {
			cartArr.map((item, index) => {
				NewArr.push(
					Object.assign({}, item, {
						isCheck: false,
					})
				)
			})
			this.youhuiprice = 0
			this.iscount = 0
			this.wuyefei = 0
			this.manzengGoods = ''
			this.cartList = NewArr
			this.$apply()
		}

	}
	// 计算满减活动
	async jisuanManjian() {
		var cartList = this.cartList
		var cart_id_list = ''
		for (var i = 0; i < cartList.length; i++) {
			if (cartList[i].isCheck) {
				cart_id_list += cartList[i].id + ','
			}
		}
		cart_id_list = cart_id_list.substr(0, cart_id_list.length - 1)
		if (cart_id_list != '') {
			const {
				data: res
			} = await wepy.post('baseRest/getShoppingCartListPrice', {
				cart_id_list,
				memberId: this.userId,
				payType: '0',
				areaId:''
			})
			console.log(res)
			if (res.state != 1) {
				return wepy.noToast(res.message)
			} else {
				if (res.data.totalPrice) {
					this.iscount = res.data.totalPrice
					this.youhuiprice = res.data.youhuiPrice
					this.wuyefei = res.data.wuyefei
					this.manzengGoods = res.data.manzengGoods
					this.manzengGoods = this.manzengGoods.substr(0, this.manzengGoods.length - 1).split(',')
					this.$apply()
					console.log(res.data)

					this.$parent.globalData.cartYouhuinfo = res.data
				}
			}
		} else {
			this.iscount = 0
			this.youhuiprice = 0
			this.manzengGoods = ''
			this.wuyefei = 0
		}
	}
	// 数量加减
	async numAvtive(num, id, isCheck, skuAttrValueIds) {
		const {
			data: res
		} = await wepy.post('baseRest/updateShoppingCart', {
			num: JSON.stringify(num),
			id: id,
			skuAttrValueIds: skuAttrValueIds
		})
		var that = this;
		if (res.state == 0) {
			wepy.noToast(res.message)
		} else {
			this.getCartList()
			if (isCheck) {
				that.jisuanManjian()
			}
		}
	}
}
